package com.njwb.day06.Students;

import java.util.Arrays;
import java.util.Scanner;

public class StudentInformationSystem {


    static Student[] stus=new Student[0];
    public static void main(String[] args) {

         /*
            学生信息录入系统
            用于存放学生学号的数组：int[] nos;
            用于存放学生姓名的数组 String[] names;
            用于存放学生成绩的数组 double[] scores;
            功能：
            1.添加学生信息：学号、姓名、成绩
                          学号不能重复
            2.修改学生信息：根据学号修改姓名和成绩
                          学号是否存在的判断
            3.删除学生信息：根据学号删除对应学生
                          学号是否存在的判断
            4.查询所有学生信息
            5.根据学号查询学生信息
            6.根据姓名查询学生信息
            7.根据学生的成绩进行从大到小排序展示
            8.退出系统 System.exit(0)
            要求：
            对于正常操作的数据根据以上三个数组进行正常录入或查询
            对于条件不满足的一些情形通过错误对用户做出错误提示
         */
        /*        Scanner sc=new Scanner(System.in);*/
        //int:sc.nextInt();
        //String:sc.nextLine();
        //double:sc.nextDouble();
        Scanner sc = new Scanner(System.in);
        initStudents();
        while(true) {
            System.out.println("""
                    1.添加学生信息：学号、姓名、成绩
                    2.修改学生信息：根据学号修改姓名和成绩
                    3.删除学生信息：根据学号删除对应学生
                    4.查询所有学生信息
                    5.根据学号查询学生信息
                    6.根据姓名查询学生信息
                    7.根据学生的成绩进行从大到小排序展示
                    8.退出系统
                    请输入操作：""");

            int x = sc.nextInt();
            switch(x) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    alterStudent();
                    break;
                case 3:
                    deleteStudent();
                    break;
                case 4:
                    showStudents();
                    break;
                case 5:
                    showNo();
                    break;
                case 6:
                    showName();
                    break;
                case 7:
                    showDesc();
                    break;
                case 8:
                    System.exit(0);break;
                default:
                    System.out.println("输入错误，请重新输入！");
            }
        }
    }


    private static void initStudents() {
        Student s1 = new Student();
        s1.setNo(10001);
        s1.setName("张三");
        s1.setScores(88.5);


        Student s2 = new Student();
        s2.setNo(10002);
        s2.setName("李四");
        s2.setScores(92.0);

        Student s3 = new Student();
        s3.setNo(10003);
        s3.setName("王五");
        s3.setScores(79.5);

        Student s4 = new Student();
        s4.setNo(10004);
        s4.setName("赵六");
        s4.setScores(85.0);

        stus = new Student[]{s1, s2, s3, s4};
    }
//添加学生
    private static void addStudent() {


        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学号：");
        int no = sc.nextInt();


        for (int i = 0; i < stus.length; i++) {
            if (stus[i].getNo() == no) {
                System.out.println("学号已存在！！！");
                return;
            }
        }

        System.out.println("请输入姓名：");
        String name = sc.next();
        System.out.println("请输入成绩：");
        double score = sc.nextDouble();

        stus = Arrays.copyOf(stus, stus.length + 1);
        stus[stus.length - 1] = new Student();
        stus[stus.length - 1].setNo(no) ;
        stus[stus.length - 1].setName(name);
        stus[stus.length - 1].setScores(score);

        System.out.println("添加成功！！！");
    }
//改
    private static void alterStudent() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改的学生学号：");
        int no = sc.nextInt();

        for (int i = 0; i < stus.length; i++) {
            if (stus[i].getNo() == no) {
                System.out.println("当前姓名：" + stus[i].getName() + "，请输入新姓名：");
                stus[i].setName(sc.next());
                System.out.println("当前成绩：" + stus[i].getScores() + "，请输入新成绩：");
                stus[i].setScores(sc.nextDouble());
                System.out.println("修改成功！");
                return;
            }
        }
        System.out.println("学号不存在！");
    }
    //删
    private static void deleteStudent() {


        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的学生学号：");
        int no = sc.nextInt();

        for (int i = 0; i < stus.length; i++) {
            if (stus[i].getNo() == no) {
                Student[] newstus=new Student[stus.length-1];

                System.arraycopy(stus, 0, newstus, 0, i);


                System.arraycopy(stus, i + 1, newstus, i, stus.length - i - 1);


                stus = newstus;


                System.out.println("删除成功！");
                return;
            }
        }
        System.out.println("学号不存在！");
    }
    //查
    private static void showStudents() {
        System.out.println("学号\t    姓名\t  成绩");
        for (int i = 0; i < stus.length; i++) {
            System.out.println(stus[i].getNo() + "\t" + stus[i].getName() + "\t  " +stus[i].getScores());
        }
    }
    //根据学号查
    private static void showNo() {

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要查询的学号：");
        int no = sc.nextInt();

        for (int i = 0; i < stus.length; i++) {
            if (stus[i].getNo() == no) {
                System.out.println("学号\t    姓名\t  成绩");
                System.out.println(stus[i].getNo() + "\t" + stus[i].getName() + "\t  " +stus[i].getScores());
                return;
            }
        }
        System.out.println("学号不存在！");
    }
    //根据姓名查
    private static void showName() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要查询的姓名：");
        String name = sc.next();

        boolean found = false;
        System.out.println("学号\t    姓名\t  成绩");
        for (int i = 0; i < stus.length; i++) {
            if (stus[i].getName().equals(name)) {
                System.out.println(stus[i].getNo() + "\t" + stus[i].getName() + "\t  " +stus[i].getScores());
                found = true;
            }
        }
        if (!found) {
            System.out.println("没有找到该姓名的学生！");
        }
    }//

    //排名
    private static void showDesc() {

        Student[] newstus = Arrays.copyOf(stus, stus.length);
        for (int i=0;i<stus.length-1;i++){
            for (int k=i;k<stus.length;k++){
                if (newstus[i].getScores()<newstus[k].getScores()){
                    Student a=newstus[i];
                    newstus[i]=newstus[k];
                    newstus[k]=a;


                }
            }
        }
        System.out.println("学号\t    姓名\t  成绩");
        for (int i = 0; i < stus.length; i++) {
            System.out.println(newstus[i].getNo() + "\t" + newstus[i].getName() + "\t  " +newstus[i].getScores());
        }
    }


   /* // 添加学生
    private static void addStudent() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学号：");
        int no = sc.nextInt();


        for (int i = 0; i < nos.length; i++) {
            if (nos[i] == no) {
                System.out.println("学号已存在！！！");
                return;
            }
        }

        System.out.println("请输入姓名：");
        String name = sc.next();
        System.out.println("请输入成绩：");
        double score = sc.nextDouble();

        nos = Arrays.copyOf(nos, nos.length + 1);
        names = Arrays.copyOf(names, names.length + 1);
        scores = Arrays.copyOf(scores, scores.length + 1);

        nos[nos.length - 1] = no;
        names[names.length - 1] = name;
        scores[scores.length - 1] = score;

        System.out.println("添加成功！！！");
    }

    // 修改学生信息
    private static void alterStudent() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改的学生学号：");
        int no = sc.nextInt();

        for (int i = 0; i < nos.length; i++) {
            if (nos[i] == no) {
                System.out.println("当前姓名：" + names[i] + "，请输入新姓名：");
                names[i] = sc.next();
                System.out.println("当前成绩：" + scores[i] + "，请输入新成绩：");
                scores[i] = sc.nextDouble();
                System.out.println("修改成功！");
                return;
            }
        }
        System.out.println("学号不存在！");
    }

    // 删除学生
    private static void deleteStudent() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的学生学号：");
        int no = sc.nextInt();

        for (int i = 0; i < nos.length; i++) {
            if (nos[i] == no) {

                int[] newNos = new int[nos.length - 1];
                String[] newNames = new String[names.length - 1];
                double[] newScores = new double[scores.length - 1];

                System.arraycopy(nos, 0, newNos, 0, i);
                System.arraycopy(names, 0, newNames, 0, i);
                System.arraycopy(scores, 0, newScores, 0, i);

                System.arraycopy(nos, i + 1, newNos, i, nos.length - i - 1);
                System.arraycopy(names, i + 1, newNames, i, names.length - i - 1);
                System.arraycopy(scores, i + 1, newScores, i, scores.length - i - 1);

                nos = newNos;
                names = newNames;
                scores = newScores;

                System.out.println("删除成功！");
                return;
            }
        }
        System.out.println("学号不存在！");
    }

    //展示学生
    private static void showStudents() {
        System.out.println("学号\t    姓名\t  成绩");
        for (int i = 0; i < nos.length; i++) {
            System.out.println(nos[i] + "\t" + names[i] + "\t  " + scores[i]);
        }
    }

    //按学号查询
    private static void showNo() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要查询的学号：");
        int no = sc.nextInt();

        for (int i = 0; i < nos.length; i++) {
            if (nos[i] == no) {
                System.out.println("学号\t    姓名\t  成绩");
                System.out.println(nos[i] + "\t" + names[i] + "\t  " + scores[i]);
                return;
            }
        }
        System.out.println("学号不存在！");
    }

    //按名字查询
    private static void showName() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要查询的姓名：");
        String name = sc.next();

        boolean found = false;
        System.out.println("学号\t    姓名\t  成绩");
        for (int i = 0; i < names.length; i++) {
            if (names[i].equals(name)) {
                System.out.println(nos[i] + "\t" + names[i] + "\t  " + scores[i]);
                found = true;
            }
        }
        if (!found) {
            System.out.println("没有找到该姓名的学生！");
        }
    }


    private static void showDesc() {
        int[] newNos = Arrays.copyOf(nos, nos.length);
        String[] newNames = Arrays.copyOf(names, names.length);
        double[] newScores = Arrays.copyOf(scores, scores.length);
        for (int i=0;i<nos.length-1;i++){
            for (int k=i;k<nos.length;k++){
                if (newScores[i]<newScores[k]){
                    int aa=newNos[i];
                    String bb=newNames[i];
                    double cc=newScores[i];
                    newNos[i]=newNos[k];
                    newNos[k]=aa;
                    newNames[i]=newNames[k];
                    newNames[k]=bb;
                    newScores[i]=newScores[k];
                    newScores[k]=cc;

                }
            }
        }
        System.out.println("学号\t    姓名\t  成绩");
        for (int i = 0; i < nos.length; i++) {
            System.out.println(newNos[i] + "\t" + newNames[i] + "\t  " + newScores[i]);
        }

    }

*/

}